import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.Timer;


public class PlayerTest extends JFrame implements ActionListener {
	private Player p = new Player(150, 250, 30, Color.orange);
	private Timer update = new Timer(35, this);
	private Timer speed = new Timer(60, this);
	public Boolean xSpeedInc = false;
	public Boolean xSpeedDec = false;
	public Boolean ySpeedInc = false;
	public Boolean ySpeedDec = false;
	
	public PlayerTest() {
		addKeyListener(kl);
		update.start();
		speed.start();
		setResizable(false);
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(1000, 500));
		setFocusable(true);
		pack();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void paint(Graphics g) {
		super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(p.getColor());
        g2d.fillOval(p.getX(), p.getY(), p.getRadius(), p.getRadius());
        
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == update) {
			repaint();
		}
		if (e.getSource() == speed) {
			if (ySpeedInc) {
				p.ySpeedInc();
			}
			if (ySpeedDec) {
				p.ySpeedDec();
			}
			if (xSpeedInc) {
				p.xSpeedInc();
			}
			if (xSpeedDec) {
				p.xSpeedDec();
			}
		}
		
		p.updatePlayer();
	}
	
	KeyListener kl = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			//Player 2:
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				ySpeedDec = true;
			
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				ySpeedInc = true;
			
			}
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				xSpeedDec = true;
			
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				xSpeedInc = true;
			
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				ySpeedDec = false;
			
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				ySpeedInc = false;
		
			}
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				xSpeedDec = false;
				
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				xSpeedInc = false;
				
			}
		}
	};
	
	public static void main (String[] arg) {
		new PlayerTest();
	}
	

}